﻿package rd.hardcore.engine.update 
{
	import air.update.ApplicationUpdaterUI;
	import air.update.core.UpdaterConfiguration;
	import air.update.core.UpdaterHSM;
	import air.update.events.StatusUpdateEvent;
	import air.update.events.UpdateEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	
	
	/**
	 * Mise à jour automatique
	 * @author Thibault Perret
	 */
	public class ApplicationUpdater extends EventDispatcher
	{
		public var appUpdater:ApplicationUpdaterUI;
		
		public function ApplicationUpdater() 
		{
			appUpdater = new ApplicationUpdaterUI();
		}
		
		private function updateCheckNow(e:UpdateEvent):void 
		{
			
			readyToCheck();
			UpdaterConfiguration
		}
		
		
		
		private function readyToCheck():void
		{
			appUpdater.checkNow();
		}
		
		
		public function Maj(_maj_url:String):void
		{
			appUpdater.addEventListener(UpdateEvent.INITIALIZED, updateCheckNow);
			appUpdater.addEventListener(UpdateEvent.BEFORE_INSTALL, beforeInstall);
			appUpdater.addEventListener(UpdateEvent.DOWNLOAD_COMPLETE, downloadComplete);
			appUpdater.addEventListener(UpdateEvent.DOWNLOAD_START, downloadStart );
			appUpdater.addEventListener(UpdateEvent.CHECK_FOR_UPDATE, checkForUpdate );
			appUpdater.addEventListener(StatusUpdateEvent.UPDATE_STATUS, updateStatus);
			appUpdater.updateURL = _maj_url;
			appUpdater.delay = 1;
			appUpdater.isCheckForUpdateVisible = false;
			appUpdater.initialize();
		}
		
		private function updateStatus(e:StatusUpdateEvent):void 
		{
			trace("statusEvent"+e.available);
			trace("statusEvent"+e.details);
		}
		
		private function checkForUpdate(e:UpdateEvent):void 
		{
			trace("checkForUpdate");
		}
		
		private function downloadStart(e:UpdateEvent):void 
		{
			trace("downloadStart");
		}
		
		private function downloadComplete(e:UpdateEvent):void 
		{
			trace("downloadComplete");
		}
		
		private function beforeInstall(e:UpdateEvent):void 
		{
			trace("beforeInstall");
		}
		
		/**
		 * Vérification de la dernière version de l'application pour téléchargement et mise à jour
		 */
		/*private function updateChecker():void
		{
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
			loader.load(new URLRequest(maj_url));
		}
		
		private function IOErrorHandler(e:IOErrorEvent):void 
		{
			trace("0: Pas d'accès au fichier distant");
		}
		
		private function onComplete(e:Event):void 
		{
			trace("loader: " + loader.data);
			var xml:XMLList = new XMLList(loader.data);
			trace("desc : " + xml.url);
		}*/
		
	}

}